<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='BUI'>/**
</span> * @fileOverview 表单验证
 * @ignore
 */

var BUI = require(&#39;bui-common&#39;),
  Rules = require(&#39;./rules&#39;);

<span id='BUI-Form-ValidView'>/**
</span> * @class BUI.Form.ValidView
 * @private
 * 对控件内的字段域进行验证的视图
 */
var ValidView = function(){

};

ValidView.prototype = {
<span id='BUI-Form-ValidView-method-getErrorsContainer'>  /**
</span>   * 获取错误信息的容器
   * @protected
   * @return {jQuery} 
   */
  getErrorsContainer : function(){
    var _self = this,
      errorContainer = _self.get(&#39;errorContainer&#39;);
    if(errorContainer){
      if(BUI.isString(errorContainer)){
        return _self.get(&#39;el&#39;).find(errorContainer);
      }
      return errorContainer;
    }
    return _self.getContentElement();
  },
<span id='BUI-Form-ValidView-method-showErrors'>  /**
</span>   * 显示错误
   */
  showErrors : function(errors){
    var _self = this,
      errorsContainer = _self.getErrorsContainer(),
      errorTpl = _self.get(&#39;errorTpl&#39;);     
    _self.clearErrors(); 

    if(!_self.get(&#39;showError&#39;)){
      return ;
    }
    //如果仅显示第一条错误记录
    if(_self.get(&#39;showOneError&#39;)){
      if(errors &amp;&amp; errors.length){
        _self.showError(errors[0],errorTpl,errorsContainer);
      }
      return ;
    }

    BUI.each(errors,function(error){
      if(error){
        _self.showError(error,errorTpl,errorsContainer);
      }
    });
  },
<span id='BUI-Form-ValidView-method-showError'>  /**
</span>   * 显示一条错误
   * @protected
   * @template
   * @param  {String} msg 错误信息
   */
  showError : function(msg,errorTpl,container){

  },
<span id='BUI-Form-ValidView-method-clearErrors'>  /**
</span>   * @protected
   * @template
   * 清除错误
   */
  clearErrors : function(){

  }
};
<span id='BUI-Form-Valid'>/**
</span> * 对控件内的字段域进行验证
 * @class  BUI.Form.Valid
 */
var Valid = function(){

};

Valid.ATTRS = {

<span id='BUI-Form-Valid-property-defaultRules'>  /**
</span>   * 控件固有的验证规则，例如，日期字段域，有的date类型的验证
   * @protected
   * @type {Object}
   */
  defaultRules : {
    value : {}
  },
<span id='BUI-Form-Valid-property-defaultMessages'>  /**
</span>   * 控件固有的验证出错信息，例如，日期字段域，不是有效日期的验证字段
   * @protected
   * @type {Object}
   */
  defaultMessages : {
    value : {}
  },
<span id='BUI-Form-Valid-property-rules'>  /**
</span>   * 验证规则
   * @type {Object}
   */
  rules : {
    shared : false,
    value : {}
  },
<span id='BUI-Form-Valid-property-messages'>  /**
</span>   * 验证信息集合
   * @type {Object}
   */
  messages : {
    shared : false,
    value : {}
  },
<span id='BUI-Form-Valid-property-validator'>  /**
</span>   * 验证器 验证容器内的表单字段是否通过验证
   * @type {Function}
   */
  validator : {

  },
<span id='BUI-Form-Valid-property-errorContainer'>  /**
</span>   * 存放错误信息容器的选择器，如果未提供则默认显示在控件中
   * @private
   * @type {String}
   */
  errorContainer : {
    view : true
  },
<span id='BUI-Form-Valid-property-errorTpl'>  /**
</span>   * 显示错误信息的模板
   * @type {Object}
   */
  errorTpl : {
    view : true,
    value : &#39;&lt;span class=&quot;x-field-error&quot;&gt;&lt;span class=&quot;x-icon x-icon-mini x-icon-error&quot;&gt;!&lt;/span&gt;&lt;label class=&quot;x-field-error-text&quot;&gt;{error}&lt;/label&gt;&lt;/span&gt;&#39;
  },
<span id='BUI-Form-Valid-property-showError'>  /**
</span>   * 显示错误
   * @type {Boolean}
   */
  showError : {
    view : true,
    value : true
  },
<span id='BUI-Form-Valid-property-showOneError'>  /**
</span>   * 是否仅显示一个错误
   * @type {Boolean}
   */
  showOneError: {

  },
<span id='BUI-Form-Valid-property-error'>  /**
</span>   * 错误信息，这个验证错误不包含子控件的验证错误
   * @type {String}
   */
  error : {

  },
<span id='BUI-Form-Valid-property-pauseValid'>  /**
</span>   * 暂停验证
   * &lt;pre&gt;&lt;code&gt;
   *   field.set(&#39;pauseValid&#39;,true); //可以调用field.clearErrors()
   *   field.set(&#39;pauseValid&#39;,false); //可以同时调用field.valid()
   * &lt;/code&gt;&lt;/pre&gt;
   * @type {Boolean}
   */
  pauseValid : {
    value : false
  }
};

Valid.prototype = {

  __bindUI : function(){
    var _self = this;
    //监听是否禁用
    _self.on(&#39;afterDisabledChange&#39;,function(ev){
      
        var disabled = ev.newVal;
        if(disabled){
          _self.clearErrors(false,false);
        }else{
          _self.valid();
        }
    });
  },
<span id='BUI-Form-Valid-method-isValid'>  /**
</span>   * 是否通过验证
   * @template
   * @return {Boolean} 是否通过验证
   */
  isValid : function(){

  },
<span id='BUI-Form-Valid-method-valid'>  /**
</span>   * 进行验证
   */
  valid : function(){

  },
<span id='BUI-Form-Valid-method-validControl'>  /**
</span>   * @protected
   * @template
   * 验证自身的规则和验证器
   */
  validControl : function(){

  },
  //验证规则
  validRules : function(rules,value){
    if(!rules){
      return null;
    }
    if(this.get(&#39;pauseValid&#39;)){
      return null;
    }
    var _self = this,
      messages = _self._getValidMessages(),
      error = null;

    for(var name in rules){
      if(rules.hasOwnProperty(name)){
        var baseValue = rules[name];
        error = Rules.valid(name,value,baseValue,messages[name],_self);
        if(error){
          break;
        }
      }
    }
    return error;
  },
  //获取验证错误信息
  _getValidMessages : function(){
    var _self = this,
      defaultMessages = _self.get(&#39;defaultMessages&#39;),
      messages = _self.get(&#39;messages&#39;);
    return BUI.merge(defaultMessages,messages);
  },
<span id='BUI-Form-Valid-method-getValidError'>  /**
</span>   * @template
   * @protected
   * 控件本身是否通过验证，不考虑子控件
   * @return {String} 验证的错误
   */
  getValidError : function(value){
    var _self = this,
      validator = _self.get(&#39;validator&#39;),
      error = null;

    error = _self.validRules(_self.get(&#39;defaultRules&#39;),value) || _self.validRules(_self.get(&#39;rules&#39;),value);

    if(!error &amp;&amp; !this.get(&#39;pauseValid&#39;)){
      if(_self.parseValue){
        value = _self.parseValue(value);
      }
      error = validator ? validator.call(this,value) : &#39;&#39;;
    }

    return error;
  },
<span id='BUI-Form-Valid-method-getErrors'>  /**
</span>   * 获取验证出错信息，包括自身和子控件的验证错误信息
   * @return {Array} 出错信息
   */
  getErrors : function(){

  },
<span id='BUI-Form-Valid-method-showErrors'>  /**
</span>   * 显示错误
   * @param {Array} errors 显示错误
   */
  showErrors : function(errors){
    var _self = this,
      errors = errors || _self.getErrors();
    _self.get(&#39;view&#39;).showErrors(errors);
  },
<span id='BUI-Form-Valid-method-clearErrors'>  /**
</span>   * 清除错误
   * @param {Boolean} reset 清除错误时是否重置
   * @param {Boolean} [deep = true] 是否清理子控件的错误 
   */
  clearErrors : function(reset,deep){
    deep = deep == null ? true : deep;
    var _self = this,
      children = _self.get(&#39;children&#39;);
    if(deep){
      BUI.each(children,function(item){
        if(item.clearErrors){
          if(item.field){
            item.clearErrors(reset);
          }else{
            item.clearErrors(reset,deep);
          }
        }
      });
    }
    
    _self.set(&#39;error&#39;,null);
    _self.get(&#39;view&#39;).clearErrors();
  },
<span id='BUI-Form-Valid-method-addRule'>  /**
</span>   * 添加验证规则
   * @param {String} name 规则名称
   * @param {*} [value] 规则进行校验的进行对比的值，如max : 10 
   * @param {String} [message] 出错信息,可以使模板
   * &lt;ol&gt;
   *   &lt;li&gt;如果 value 是单个值，例如最大值 value = 10,那么模板可以写成： &#39;输入值不能大于{0}!&#39;&lt;/li&gt;
   *   &lt;li&gt;如果 value 是个复杂对象，数组时，按照索引，对象时按照 key 阻止。如：value= {max:10,min:5} ，则&#39;输入值不能大于{max},不能小于{min}&#39;&lt;/li&gt;
   * &lt;/ol&gt;
   *         var field = form.getField(&#39;name&#39;);
   *         field.addRule(&#39;required&#39;,true);
   *
   *         field.addRule(&#39;max&#39;,10,&#39;不能大于{0}&#39;);
   */
  addRule : function(name,value,message){
    var _self = this,
      rules = _self.get(&#39;rules&#39;),
      messages = _self.get(&#39;messages&#39;);
    rules[name] = value;
    if(message){
      messages[name] = message;
    }
    
  },
<span id='BUI-Form-Valid-method-addRules'>  /**
</span>   * 添加多个验证规则
   * @param {Object} rules 多个验证规则
   * @param {Object} [messages] 验证规则的出错信息
   *         var field = form.getField(&#39;name&#39;);
   *         field.addRules({
   *           required : true,
   *           max : 10
   *         });
   */
  addRules : function(rules,messages){
    var _self = this;

    BUI.each(rules,function(value,name){
      var msg = messages ? messages[name] : null;
      _self.addRule(name,value,msg);
    });
  },
<span id='BUI-Form-Valid-method-removeRule'>  /**
</span>   * 移除指定名称的验证规则
   * @param  {String} name 验证规则名称
   *         var field = form.getField(&#39;name&#39;);
   *         field.remove(&#39;required&#39;);   
   */
  removeRule : function(name){
    var _self = this,
      rules = _self.get(&#39;rules&#39;);
    delete rules[name];
  },
<span id='BUI-Form-Valid-method-clearRules'>  /**
</span>   * 清理验证规则
   */
  clearRules : function(){
    var _self = this;
    _self.set(&#39;rules&#39;,{});
  }
};

Valid.View = ValidView;

module.exports = Valid;
</pre>
</body>
</html>
